package com.mo.demo.jsr107;

import org.ehcache.core.config.DefaultConfiguration;
import org.ehcache.impl.config.persistence.DefaultPersistenceConfiguration;
import org.ehcache.jsr107.EhcacheCachingProvider;

import javax.cache.CacheManager;
import javax.cache.Caching;
import javax.cache.spi.CachingProvider;
import java.io.File;

public class Jsr107EhcacheDemo2 {
    public static void main(String[] args) {
        CachingProvider cachingProvider = Caching.getCachingProvider();
        // 由于现在 Ehcache是JCache的实现，所以可以强制转化为 EhcacheCachingProvider
        EhcacheCachingProvider ehcacheProvider = (EhcacheCachingProvider) cachingProvider;

        // 使用特定的 Ehcache 创建一个配置DefaultConfiguration并传递一些CacheManager级别的配置
        DefaultConfiguration configuration = new DefaultConfiguration(ehcacheProvider.getDefaultClassLoader(),
                new DefaultPersistenceConfiguration(getPersistenceDirectory()));

        // CacheManager使用将 Ehcache 配置作为参数的方法创建。
        CacheManager cacheManager = ehcacheProvider.getCacheManager(ehcacheProvider.getDefaultURI(), configuration);


    }

    private static File getPersistenceDirectory() {
        return new File("/dir/to/persistence/");
    }
}
